<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 组织架构内容 - 头部 -->
      <el-card class="tree-card">
        <!-- 放置结构内容 -->
        <tree-tools
          :tree-node="company"
          :is-root="true"
          @addDept="addDept"
        />
        <!-- 放置el-tree -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- el-tree传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scoped="obj" 接收传递给插槽的数据 data是解构了obj - 每个节点的数据对象 -->
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <tree-tools
            slot-scope="{data}"
            :tree-node="data"
            @addDept="addDept"
            @delDepts="loadDepartments"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
      <!-- 新增弹窗组件 -->
      <AddDept
        ref="editDepts"
        :tree-node="node"
        :show-dialog.sync="isAddDeptShow"
        @addDepts="loadDepartments()"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './componments/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeDate } from '@/utils'
import AddDept from './componments/add-dept.vue'
export default {
  name: 'DepartMents',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      // 树形结构 数据列表
      departs: [
        {
          name: '总裁办',
          manager: '孙才'
        },
        {
          name: '行政部',
          manager: '罗晓晓'
        },
        {
          name: '人事部',
          manager: '文吉星'
        },
        {
          name: '财务部',
          children: [
            {
              name: '财务核算部'
            },
            {
              name: '税务管理部'
            },
            {
              name: '薪资管理部'
            }
          ]
        },
        {
          name: '技术部',
          children: [
            {
              name: 'Java研发部'
            },
            {
              name: 'Python研发部'
            },
            {
              name: 'Php研发部'
            }
          ]
        },
        {
          name: '运营部'
        },
        {
          name: '市场部',
          manager: '武高丽',
          children: [
            {
              name: '北京事业部'
            },
            {
              name: '伤害事业部'
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人'
      },
      isAddDeptShow: false, // 控制新增部门弹窗出现
      node: null // 记录点击添加部门的node节点
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const result = await getDepartments()
        this.company = { name: result.companyName, manager: '负责人', id: '' }
        this.departs = tranListToTreeDate(result.depts, '')
        // console.log(result)
      } catch (error) {
        console.log(error)
      }
    },
    // 添加部门 - 展开添加面板并传点击节点数据
    addDept(treeNode) {
      this.isAddDeptShow = true
      this.node = treeNode // 点击的节点
    },
    // 编辑部门
    editDepts(treeNode) {
      this.isAddDeptShow = true
      this.node = treeNode
      this.$refs.editDepts.loadDepartDetail(treeNode.id)
      // console.log(this.$refs)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
